package com.ensah.web.actions;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.boudaa.dao.exceptions.EntityNotFoundException;
import com.ensah.metier.bo.Absence;
import com.ensah.metier.bo.Attestation;
import com.ensah.metier.bo.Classe;
import com.ensah.metier.bo.Etudiant;
import com.ensah.metier.bo.Role;
import com.ensah.service.AttestationService;
import com.ensah.service.ClasseService;
import com.ensah.service.EtudiantService;
import com.ensah.service.RoleService;
import com.opensymphony.xwork2.ActionSupport;

public class EtudiantAction extends ActionSupport{
	
	
	private String cne;
	private String nom;
	private String prenom;
	private String cin;
	private String adresse;
	private String tel;
	private String email;
	private boolean sexe;
	private int genre;
	private String g="Homme";
	private String login;
	private String password;
	private Date dateNaiss;
	private boolean etat;
	private Date date;
	

	private String num;
	private String nomClasse;
	private List<Etudiant> listeEtd;
	private List<Attestation> listeAtt;
	private List<String> listeClasses;
	private EtudiantService etd;
	private ClasseService c;
	private AttestationService at;
	private RoleService r;
	
	
	
	public String showForm(){
		
		listeClasses=c.getClassesName();
		
		return "success";
	}
	
	public String ajouterEtd(){
		
		
	     Role role=r.getRoleByNom("etudiant");
     
	     Classe cl=null;
         login=cne;
         password=cne;
         int n=Integer.parseInt(num);
        
         try{
        	 
        	 cl=c.getClassebyNom(nomClasse);
        
         
         if(genre==2) {
        	 
        	 sexe=true;
        	 g="Femme";
         }
         
	     Etudiant et=new Etudiant(nom, prenom, cin, adresse, tel, email, dateNaiss, login, password, role, true, false, false,sexe, n, cne, cl, new ArrayList<Absence>(), new ArrayList<Attestation>());
		
		 
		 listeEtd=c.getEtdClasse(cl.getIdClasse());
	
		listeEtd.add(et);
		 cl.setEtudiants(listeEtd);
		 etd.saveEtudiant(et);
		 c.update(cl);
        

         } catch (EntityNotFoundException e) {
        	 return "error";
		}
        
         return "success";
	}
	
	public String supprimerEtd(){
		
		Etudiant e;
		try {
			e = etd.getEtudiantByCne(cne);
			etd.supprimerEtudiant(e.getIdUtil());
		} catch (EntityNotFoundException e1) {
			
			return "error";
		}
		
       catch (IndexOutOfBoundsException e1) {
			
			return "error";
		}
		
		
		return "success";
		
	}
	
    public String modifierEtd(){
		
		Etudiant e;
		try {
			e = etd.getEtudiantByCne(cne);
			e.setNom(nom);
	        e.setPrenom(prenom);
	        e.setNumEtd(Integer.parseInt(num));
	        e.setAdresse(adresse);
	        e.setEmail(email);
	        e.setTel(tel);
	        e.setCin(cin);
	        e.setCne(cne);
	      
	        
	        if(genre==2) e.setSexe(true);
	        else e.setSexe(false);
	        
	        Classe cl=c.getClassebyNom(nomClasse);
			listeEtd=c.getEtdClasse(cl.getIdClasse());
			
			listeEtd.add(e);
			e.setClasse(cl);
			 cl.setEtudiants(listeEtd);
			 
			 c.update(cl);
		} catch (EntityNotFoundException e1) {
			
			
			return "error";
		}
		 catch (IndexOutOfBoundsException e1) {
				
				return "error";
			}
        
      
        e.setDateNaiss(dateNaiss);
   
		etd.modifierEtudiant(e);
		
		
		return "success";
		
	}
    

    public String modEtd(){
		   

    	Etudiant e;
		try {
			e = etd.getEtudiantByCne(cne);
			
			nom=e.getNom();
			prenom=e.getPrenom();
			
			 cin=e.getCin();
			 adresse=e.getAdresse();
			tel=e.getTel();
			email=e.getEmail();
		
			if(e.getSexe()==true) {
				genre=2;
			}
			
			else genre=1;
			
			dateNaiss=e.getDateNaiss();
			num=String.valueOf(e.getNumEtd());
			listeClasses=c.getClassesName();
			Classe classe=c.getClasseById(e.getClasse().getIdClasse());
			 nomClasse=classe.getNom();
			
		} catch (EntityNotFoundException e1) {


			return "error";
		}
	
		catch (IndexOutOfBoundsException e1) {
			
			return "error";
		}
		
		
	   
		
		return "success";
         
	}
    
	public String rechCne(){
			   

    	Etudiant e;
		try {
			e = etd.getEtudiantByCne(cne);
			nom=e.getNom();
			prenom=e.getPrenom();
			if(e.getSexe()==true) g="Femme";
			cin=e.getCin();
			 adresse=e.getAdresse();
			tel=e.getTel();
			email=e.getEmail();
			dateNaiss=e.getDateNaiss();
			num=String.valueOf(e.getNumEtd());
		
			Classe classe;
			
				classe = c.getClasseById(e.getClasse().getIdClasse());
				 nomClasse=classe.getNom();
			
		} catch (EntityNotFoundException e1) {
			
			return "error";
		}
	
         catch (IndexOutOfBoundsException e1) {
			
			return "error";
		}

		
		return "success";
         
	}

	public String rechNom() {
		   

       try {
		listeEtd=etd.getEtudiantByNom(nom);
	
	} catch (EntityNotFoundException e) {
		
		return "error";
	}
       catch (IndexOutOfBoundsException e1) {
			
			return "error";
		}
      
		return "success";
	}
	
	public String listeEtd(){
		   

		try{
		Classe cl=c.getClassebyNom(nomClasse);
		System.out.println("nommm"+nomClasse);
		Long idClasse=cl.getIdClasse();
		System.out.println("idClasse"+idClasse);
		listeEtd=c.getEtdClasse(idClasse);
		System.out.println("listeEtd"+listeEtd);
		
		}
		catch(EntityNotFoundException e){
			
			return "error";
		}
		
        catch (IndexOutOfBoundsException e1) {
			
			return "error";
		}
		
		return "success";
	}
	
	
	public String ajouterAtt(){
		
		date=new Date();
		etat=false;
		Etudiant e;
		try {
			e = etd.getEtudiantByCne(cne);
			Attestation att=new Attestation(etat, date, e);
			
			listeAtt=at.getListeAttByCne(cne);
			
			listeAtt.add(att);
			 e.setAttestations(listeAtt);
			 at.saveAttestation(att);
			 etd.modifierEtudiant(e);
			
		} catch (EntityNotFoundException e1) {
			
			
			return "error";
		}
		
		return "success";
		
		
	}
	public String listeAtt(){
		   

		listeAtt=at.getListeAtt();
		
		for(Attestation a:listeAtt){
			
			System.out.println("attestation"+a.getIdAt());
		}
		
		return "success";
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getNomClasse() {
		return nomClasse;
	}

	public void setNomClasse(String nomClasse) {
		this.nomClasse = nomClasse;
	}
	
	
	public String getCin() {
		return cin;
	}

	public void setCin(String cin) {
		this.cin = cin;
	}

	public String getAdresse() {
		return adresse;
	}

	public void setAdresse(String adresse) {
		this.adresse = adresse;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean getSexe() {
		return sexe;
	}

	public void setSexe(boolean sexe) {
		this.sexe = sexe;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getDateNaiss() {
		return dateNaiss;
	}

	public void setDateNaiss(Date dateNaiss) {
		this.dateNaiss = dateNaiss;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	

	public RoleService getR() {
		return r;
	}

	public void setR(RoleService r) {
		this.r = r;
	}

	
	
	public ClasseService getC() {
		return c;
	}

	public void setC(ClasseService c) {
		this.c = c;
	}

	public List<Etudiant> getListeEtd() {
		
		
		return listeEtd;
	}

	public void setListeEtd(List<Etudiant> listeEtd) {
		this.listeEtd = listeEtd;
	}

	public String getCne() {
		return cne;
	}

	public void setCne(String cne) {
		this.cne = cne;
	}

	public EtudiantService getEtd() {
		return etd;
	}

	public void setEtd(EtudiantService etd) {
		this.etd = etd;
	}

	public int getGenre() {
		return genre;
	}

	public void setGenre(int genre) {
		this.genre = genre;
	}

	public List<String> getListeClasses() {
		listeClasses=c.getClassesName();
		return listeClasses;
	}

	public void setListeClasses(List<String> listeClasses) {
		this.listeClasses = listeClasses;
	}

	public String getG() {
		return g;
	}

	public void setG(String g) {
		this.g = g;
	}

	public List<Attestation> getListeAtt() {
		listeAtt=at.getListeAtt();

		return listeAtt;
	}

	public void setListeAtt(List<Attestation> listeAtt) {
		this.listeAtt = listeAtt;
	}

	public AttestationService getAt() {
		
		return at;
	}

	public void setAt(AttestationService at) {
		this.at = at;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
	public boolean isEtat() {
		return etat;
	}

	public void setEtat(boolean etat) {
		this.etat = etat;
	}

	

}
